var animationContext;
var timer;
var n;
Page({
  data: {
    animationData: {},
    img: '',
    play_url: '/images/loading.gif',
    playing: false,
    percent: 0,
    singer: '',
    time: '',
    filehash: '',
  },
  rotate: function () {
    animationContext.rotate(1 * n++);
    animationContext.step();
    this.setData({
      animationData: animationContext.export()
    })
  },
  playMusic: function (animationContext) {
    animationContext = wx.createAnimation({
      duration: 100,
      timingFunction: 'linear'
    })
    return animationContext
  },
  back: function () {
    var pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack({
      })
    } else {
      wx.navigateTo({
        url: '/pages/index/index'
      })
    }
  },
  play: function () {
    var that = this;
    if (this.data.playing == false) {
      this.audioCtx.play()
      timer = setInterval(function () {
        that.rotate();
      }, 50)
    } else {
      this.audioCtx.pause();
      clearInterval(timer)
    }
    this.setData({
      playing: !this.data.playing
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    animationContext = this.playMusic(animationContext)
    this.audioCtx = wx.createInnerAudioContext()

    this.audioCtx.onPlay(() => {

    })
    this.audioCtx.onEnded(() => {
      this.setData({
        playing: false,
        percent: 0,
        time: ''
      })
      n = 0;
      clearInterval(timer)
    })

    this.audioCtx.onStop(() => {
      this.setData({
        playing: false,
        percent: 0,
        time: ''
      })
      n = 0;
      clearInterval(timer)
    })

    this.audioCtx.onTimeUpdate((res) => {
      let percent = this.audioCtx.currentTime / this.audioCtx.duration * 100;
      let min = parseInt(this.audioCtx.currentTime / 60)
      let sec = (this.audioCtx.currentTime % 60).toFixed(2);
      sec = sec < 10 ? '0' + sec : sec;
      this.setData({
        percent: percent,
        time: min + ":" + sec,
      })
    })

    // var that = this;
    n = 0;
    // timer = setInterval(function () {
    //   that.rotate();
    // }, 50)
    wx.request({
      url: 'https://music.******.com/getSongUrl',
      data: { filehash: options.filehash },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: res => {
        this.audioCtx.src = res.data.play_url
        this.setData({
          img: res.data.img,
          play_url: res.data.play_url,
          singer: options.singer,
          filehash: options.filehash,
        })
      },
      fail: function () {

      },
      complete: function () {
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.audioCtx.pause()
    this.setData({
      playing: false,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.audioCtx.stop()
    this.setData({
      playing: false,
      time: '',
    })
    n = 0;
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  share: function () {

  },
  onShareAppMessage: function () {
    return {
      title: '一起来听歌吧',
      desc: '一起来听歌吧',
      path: 'pages/play/play?filehash=' + this.data.filehash
      + "&singer=" + this.data.singer,
    }
  }
})
